using System;
using System.Data;
using System.Configuration;
using Pheonix.Fashionzoomer.DAL;
using System.Data.SqlClient;
using System.Collections;
using Pheonix.CodeLibrary.Data;
using Pheonix.CodeLibrary;

/// <summary>
/// Summary description for Reviewer
/// </summary>

namespace Pheonix.Fashionzoomer.Entity.SqlServer2000
{
    [System.ComponentModel.DataObject]
    public class Reviewer : SqlServerDAO
    {
        #region Reviewer Private Fields

		private Int32 _id;
		private String _reviewerName;
		private String _shortDescription;
		private Decimal _rating;
		private Int32 _totalRater;
		private String _imagePath;

        #endregion

        #region Reviewer Constractors

		public Reviewer(){}

		public Reviewer
		(
			Int32 id,
			String reviewerName,
			String shortDescription,
			Decimal rating,
			Int32 totalRater,
			String imagePath

		)
		{
			this._id = id;
			this._reviewerName = reviewerName;
			this._shortDescription = shortDescription;
			this._rating = rating;
			this._totalRater = totalRater;
			this._imagePath = imagePath;
		}

        #endregion

        #region Reviewer Properties

		public Int32 Id
		{
			get{ return this._id; }
			set{ this._id = value; }
		}

		public String ReviewerName
		{
			get{ return this._reviewerName; }
			set{ this._reviewerName = value; }
		}

		public String ShortDescription
		{
			get{ return this._shortDescription; }
			set{ this._shortDescription = value; }
		}

		public Decimal Rating
		{
			get{ return this._rating; }
			set{ this._rating = value; }
		}

		public Int32 TotalRater
		{
			get{ return this._totalRater; }
			set{ this._totalRater = value; }
		}

		public String ImagePath
		{
			get{ return this._imagePath; }
			set{ this._imagePath = value; }
		}


        #endregion

        #region Reviewer Static Methods

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
        public static Int32 CreateNewReviewer
        (
			String reviewerName,
			String shortDescription,
			Decimal rating,
			Int32 totalRater,
			String imagePath

        )
        {
            //creating an empty Reviewer object
            Reviewer reviewer = new Reviewer();
            reviewer.ReviewerName = reviewerName;
			reviewer.ShortDescription = shortDescription;
			reviewer.Rating = rating;
			reviewer.TotalRater = totalRater;
			reviewer.ImagePath = imagePath;

            return Reviewer.CreateNewReviewer(reviewer);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public static bool UpdateReviewer
        (
			Int32 id,
			String reviewerName,
			String shortDescription,
			Decimal rating,
			Int32 totalRater,
			String imagePath

        )
        {
            Reviewer reviewer = Reviewer.GetReviewerById(id);
            reviewer.ReviewerName = reviewerName;
			reviewer.ShortDescription = shortDescription;
			reviewer.Rating = rating;
			reviewer.TotalRater = totalRater;
			reviewer.ImagePath = imagePath;

            return Reviewer.UpdateReviewer(reviewer);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public static bool DeleteReviewer
        (
            Int32 id
        )
        {
            //Validate Input
            if (id <= GetIdMinValue)
                throw (new ArgumentOutOfRangeException("id in Reviewer"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddParameterToSqlCmd(sqlCmd, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, id);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_REVIEWER_DELETEREVIEWER);
            GetOneData(sqlCmd);
            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return (returnValue == 0 ? false : true);

        }

        public static bool DeleteReviewerList(System.Collections.Generic.List<Int32> idListToDelete)
        {
            //Validate Input
            foreach (Int32 id in idListToDelete)
                if (id <= GetIdMinValue)
                    throw (new ArgumentOutOfRangeException("id in Reviewer"));

            //performing deletion operation //

            string csvString = Utility.MakeCSV(idListToDelete);

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddParameterToSqlCmd(sqlCmd, "@IdList", SqlDbType.NVarChar, csvString.Length, 
                ParameterDirection.Input, csvString);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_REVIEWER_DELETEREVIEWERLIST);
            GetOneData(sqlCmd);
            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return (returnValue == idListToDelete.Count ? true : false);
        }
		
        #endregion

        #region Reviewer Instance Methods
        
        public bool Save()
		{
			if(Id <= GetIdMinValue)
			{
				Int32 TempId=CreateNewReviewer(this);
				if(TempId>0)
				{
					_id=TempId;
					return true;
				}
				else
				{
					// error occurs
					return false;
				}
			}
			else
			{	//This is a update Command
				return (UpdateReviewer(this));
			}
		}

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static CustomCollection<Reviewer> GetAllReviewer()
        {
            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_REVIEWER_GETALLREVIEWER);
            GenerateCollectionFromReader result = new GenerateCollectionFromReader(GenerateReviewerCollectionFromReader);
            CustomCollection<Reviewer> objCollection = ((CustomCollection<Reviewer>)GetAllData(sqlCmd, result));

            return objCollection;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static CustomCollection<Reviewer> GetAllReviewerPaged(String orderBy, Int32 startRowIndex, Int32 maximumRows)
        {
            //Validate Input
            if (orderBy == "")
                orderBy = "Id";

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@orderby", SqlDbType.VarChar, 50, ParameterDirection.Input, orderBy);
            AddParameterToSqlCmd(sqlCmd, "@startRowIndex", SqlDbType.Int, 0, ParameterDirection.Input, startRowIndex);
            AddParameterToSqlCmd(sqlCmd, "@maximumRows", SqlDbType.Int, 0, ParameterDirection.Input, maximumRows);

            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_REVIEWER_GETALLREVIEWERPAGED);

            GenerateCollectionFromReader result = new GenerateCollectionFromReader(GenerateReviewerCollectionFromReader);
            CustomCollection<Reviewer> objCollection = ((CustomCollection<Reviewer>)GetAllData(sqlCmd, result));
            return objCollection;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static int GetAllReviewerPagedCount(String orderBy, Int32 startRowIndex, Int32 maximumRows)
        {
            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();

            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_REVIEWER_GETALLREVIEWERPAGEDCOUNT);
            GetOneData(sqlCmd);

            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return returnValue;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static Reviewer GetReviewerById(Int32 id)
        {
            if (id <= GetIdMinValue)
                throw (new ArgumentOutOfRangeException("id in Reviewer"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, id);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_REVIEWER_GETREVIEWERBYID);
            GenerateCollectionFromReader result = new GenerateCollectionFromReader(GenerateReviewerCollectionFromReader);
            CustomCollection<Reviewer> objCollection = ((CustomCollection<Reviewer>)GetAllData(sqlCmd, result));

            if (objCollection.Count > 0)
                return (Reviewer)objCollection[0];
            else
                return null;
        }

        public static Int32 CreateNewReviewer(Reviewer newReviewer)
        {
            // validate Parameters
            if (newReviewer == null)
                throw (new ArgumentNullException("newReviewer in Reviewer"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);

            AddParameterToSqlCmd(sqlCmd, "@ReviewerName", SqlDbType.NVarChar, 64, ParameterDirection.Input, newReviewer.ReviewerName);
			AddParameterToSqlCmd(sqlCmd, "@ShortDescription", SqlDbType.NText, 0, ParameterDirection.Input, newReviewer.ShortDescription);
			AddParameterToSqlCmd(sqlCmd, "@Rating", SqlDbType.Decimal, 0, ParameterDirection.Input, newReviewer.Rating);
			AddParameterToSqlCmd(sqlCmd, "@TotalRater", SqlDbType.Int, 0, ParameterDirection.Input, newReviewer.TotalRater);
			AddParameterToSqlCmd(sqlCmd, "@ImagePath", SqlDbType.NVarChar, 128, ParameterDirection.Input, newReviewer.ImagePath);

            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_REVIEWER_CREATENEWREVIEWER);
            GetOneData(sqlCmd);
            return ((Int32)sqlCmd.Parameters["@ReturnValue"].Value);

        }

        public static bool UpdateReviewer(Reviewer newReviewer)
        {
            // validate Parameters
            if (newReviewer == null)
                throw (new ArgumentNullException("newReviewer"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);

            AddParameterToSqlCmd(sqlCmd, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, newReviewer.Id);
            AddParameterToSqlCmd(sqlCmd, "@ReviewerName", SqlDbType.NVarChar, 64, ParameterDirection.Input, newReviewer.ReviewerName);
			AddParameterToSqlCmd(sqlCmd, "@ShortDescription", SqlDbType.NText, 0, ParameterDirection.Input, newReviewer.ShortDescription);
			AddParameterToSqlCmd(sqlCmd, "@Rating", SqlDbType.Decimal, 0, ParameterDirection.Input, newReviewer.Rating);
			AddParameterToSqlCmd(sqlCmd, "@TotalRater", SqlDbType.Int, 0, ParameterDirection.Input, newReviewer.TotalRater);
			AddParameterToSqlCmd(sqlCmd, "@ImagePath", SqlDbType.NVarChar, 128, ParameterDirection.Input, newReviewer.ImagePath);

            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_REVIEWER_UPDATEREVIEWER);
            GetOneData(sqlCmd);
            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return (returnValue == 0 ? false : true);
        }

        #endregion

        #region Reviewer Related Collections
        /// <summary>
        /// Creates and returns a strongly typed collection of Reviewer custom entity. 
        /// The colelction is created through iterating on the IdataReader object which contains 
        /// Reviewer information, as a set of records, similar to tabular format.
        /// </summary>
        public static IList GenerateReviewerCollectionFromReader(IDataReader returnData)
        {

            //creating the instance of Reviewer collection
            CustomCollection<Reviewer> colReviewer = new CustomCollection<Reviewer>();

            //Iterating thriugh the data reader, to generate Reviewer collection.

            //getting the Reviewer collection by iterating thru the data reader
            while (returnData.Read())
            {
                //passing the Reviewer constructor parameters from the current instance of data reader fields.
                Reviewer newReviewer = new Reviewer
                (
                    returnData["id"] == System.DBNull.Value ? GetIdMinValue : (Int32)returnData["id"],
                    returnData["reviewer_name"] == System.DBNull.Value ? String.Empty : (String)returnData["reviewer_name"],
					returnData["short_description"] == System.DBNull.Value ? String.Empty : (String)returnData["short_description"],
					returnData["rating"] == System.DBNull.Value ? Decimal.MinValue : (Decimal)returnData["rating"],
					returnData["total_rater"] == System.DBNull.Value ? Int32.MinValue : (Int32)returnData["total_rater"],
					returnData["image_path"] == System.DBNull.Value ? String.Empty : (String)returnData["image_path"]

                );
                //adding the Reviewer to the collection
                colReviewer.Add(newReviewer);
            }

            //returns the collection of Reviewer 
            return (colReviewer);
        }

        #endregion

        #region Constants and Default Values

        public static Int32 GetIdMinValue
        {
            get
            {
                return 0;
            }
        }

        //contants
        internal const string SPR_REVIEWER_CREATENEWREVIEWER = "spr_Reviewer_CreateNewReviewer";
        internal const string SPR_REVIEWER_UPDATEREVIEWER = "spr_Reviewer_UpdateReviewer";
        internal const string SPR_REVIEWER_DELETEREVIEWER = "spr_Reviewer_DeleteReviewer";
        internal const string SPR_REVIEWER_DELETEREVIEWERLIST = "spr_Reviewer_DeleteReviewerList";
        internal const string SPR_REVIEWER_GETREVIEWERBYID = "spr_Reviewer_GetReviewerById";
        internal const string SPR_REVIEWER_GETALLREVIEWER = "spr_Reviewer_GetAllReviewer";
        internal const string SPR_REVIEWER_GETALLREVIEWERPAGED = "spr_Reviewer_GetAllReviewerPaged";
        internal const string SPR_REVIEWER_GETALLREVIEWERPAGEDCOUNT = "spr_Reviewer_GetAllReviewerPagedCount";

		//Foreign key contants
		
        #endregion
    }
}
